package com.wwz.activiti.day5;

import com.wwz.activiti.pojo.Holiday;
import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 组任务测试
 * @create: 2021-04-10 10:04:33
 * @author:wwz
 */
public class GroupTest {
    public static void main(String[] args) {
        //        //1.流程部署
//        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//        //1.1 得到repositoryService
//        RepositoryService repositoryService = processEngine.getRepositoryService();
//
//        //1.2部署
//        Deployment deploy = repositoryService.createDeployment()
//                .addClasspathResource("diagram/holiday5.bpmn")
//                .name("请假流程5-流程变量")
//                .deploy();
//
//        System.out.println(deploy.getId());
//        System.out.println(deploy.getName());

        //=================================================

        //1.启动流程实例
//        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//        //1.1得到runtimeService
//        RuntimeService runtimeService = processEngine.getRuntimeService();
//        //流程定义的key myProcess_1
//        String key = "myProcess_1";
//        //1.2
//        //启动流程实例并且设置流程变量的值
//        //影响表ACT_GE_BYTEARRAY   ACT_RU_VARIABLE
//        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(key);
//
//        System.out.println(processInstance.getName());
//        System.out.println(processInstance.getProcessDefinitionId());
//        System.out.println(processInstance.getId());

        //=================================================

        //完成任务
//        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//
//        TaskService taskService = processEngine.getTaskService();
//
//        String key = "myProcess_1";
//        Task task = taskService.createTaskQuery().processDefinitionKey(key)
//                .taskAssignee("zhangsan")
//                .singleResult();
//        if(task != null){
//            taskService.complete(task.getId());
//            System.out.println("任务执行完毕");
//        }


        //=================================================

        //查询候选用户的组任务
//        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//
//        //得到taskService对象
//        TaskService taskService = processEngine.getTaskService();
//
//        //设置参数 流程定义key 候选用户
//        String key = "myProcess_1";
//        String canditateUsers="zhangsan";
//        List<Task> list = taskService.createTaskQuery()
//                .processDefinitionKey(key)
//                .taskCandidateUser(canditateUsers).list();
//
//        for (Task task : list) {
//            System.out.println(task.getProcessInstanceId());
//            System.out.println(task.getId());
//            System.out.println(task.getName());
//            System.out.println(task.getAssignee());//说明zhangsan只是候选人，不是任务执行者
//        }


        //=================================================

        //测试zhangsan用户 来拾取组任务   拾取任务就是把候选用户 转化为真正的任务负责人
//        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//
//        //processEngine
//        TaskService taskService = processEngine.getTaskService();
//        //设置参数 流程定义key 候选用户
//        String key = "myProcess_1";
//        String canditateUsers = "zhangsan";
//
//        //执行任务查询
//        Task task = taskService.createTaskQuery()
//                .processDefinitionKey(key)
//                .taskCandidateUser(canditateUsers)
//                .singleResult();
//
//        if (task != null) {
//            taskService.claim(task.getId(), canditateUsers);
//            System.out.println("任务拾取完毕！");
//        }


        //=================================================

        //归还任务  设置assignee 为 null  （也可以给到别人）


    }
}
